package com.tl.boot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tl.boot.common.annotation.RestControllerRequstMapping;
import com.tl.boot.common.annotation.WithPermission;
import com.tl.boot.common.vo.PageInfoVO;
import com.tl.boot.common.vo.ResponseVO;
import com.tl.boot.entity.permission.dto.AddResourceInfoDTO;
import com.tl.boot.entity.permission.dto.ListResourceInfoDTO;
import com.tl.boot.entity.permission.dto.UpdateResourceInfoDTO;
import com.tl.boot.entity.permission.form.*;
import com.tl.boot.entity.permission.vo.ListResourceInfoVO;
import com.tl.boot.entity.permission.vo.QueryResourceInfoVO;
import com.tl.boot.service.resource.ResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 * com.tl.boot.controller -- ResourcesController
 * Description:
 * Created by joshua_liu <a href="mailto:joshualwork@163.com"></a>
 * On 2019/9/16 11:08
 */
@Api(tags = "权限/资源  模块")
@RestControllerRequstMapping(value = "auth/res")
public class ResourcesController {
    @Autowired
    private ResourceService resourceService;

    @PostMapping("add")
    @WithPermission(role = {"sysadmin"})
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    @ApiOperation( value = "添加 权限/资源 信息", notes = "传添加资源请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "添加资源请求对象", required = true,
                    dataType = "AddResourceInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO addResourceInfo(@RequestBody AddResourceInfoForm form) {
        AddResourceInfoDTO dto = new AddResourceInfoDTO();
        BeanUtils.copyProperties(form,dto);
        dto.setCreateTime(new Date(System.currentTimeMillis()));
        dto.setResStatus(form.getResStatusEnum().getCode().shortValue());
        Long resId = resourceService.addResourceInfo(dto);
        return ResponseVO.buildSuccess(resId);
    }

    @PostMapping("delete")
    @WithPermission(role = {"sysadmin"})
    @ApiOperation( value = "删除 权限/资源 信息", notes = "传删除资源请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "删除资源请求对象", required = true,
                    dataType = "DelResourceInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    public ResponseVO delResourceInfo(@RequestBody DelResourceInfoForm form) {
        resourceService.delResourceInfo(form.getResId());
        return ResponseVO.buildSuccess();
    }

    @PostMapping("update")
    @WithPermission(role = {"sysadmin"})
    @ApiOperation( value = "更新 权限/资源 信息", notes = "传更新资源请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "更新资源请求对象", required = true,
                    dataType = "UpdateResourceInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    @Transactional(rollbackFor = Exception.class,transactionManager = "dynamicTxManager")
    public ResponseVO updateResourceInfo(@RequestBody UpdateResourceInfoForm form) {
        UpdateResourceInfoDTO dto = new UpdateResourceInfoDTO();
        BeanUtils.copyProperties(form,dto);
        dto.setResStatus(form.getResStatusEnum().getCode().shortValue());
        resourceService.updateResourceInfo(dto);
        return ResponseVO.buildSuccess();
    }

    @PostMapping("query")
    @ApiOperation( value = "查看 权限/资源 详情信息", notes = "传查看资源详情请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "查看资源详情请求对象", required = true,
                    dataType = "QueryResourceInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO<QueryResourceInfoVO> queryResourceInfo(@RequestBody QueryResourceInfoForm form) {
        QueryResourceInfoVO resourceInfoVO =resourceService.queryResourceInfo(form.getResId());
        return ResponseVO.buildSuccess(resourceInfoVO);
    }

    @PostMapping("list")
    @ApiOperation( value = "获取资源列表信息", notes = "传获取资源列表信息请求对象" )
    @ApiImplicitParams( {
            @ApiImplicitParam( name = "form", value = "获取资源列表信息请求对象", required = true,
                    dataType = "ListResourceInfoForm", paramType = "body" ),
            @ApiImplicitParam( name = "token", value = "鉴权token", required = true, dataType = "String", paramType = "header" ),
    } )
    public ResponseVO<PageInfoVO<ListResourceInfoVO>> listResourceInfo(@RequestBody ListResourceInfoForm form) {
        PageHelper.startPage(form.getPage(), form.getSize());
        ListResourceInfoDTO dto = new ListResourceInfoDTO();
        BeanUtils.copyProperties(form, dto);
        List<ListResourceInfoVO> resourceInfoVOS = resourceService.listResourceInfo(dto);
        Page<ListResourceInfoVO> page = (Page<ListResourceInfoVO>) resourceInfoVOS;
        return ResponseVO.buildSuccess(page.getResult(),page.getTotal());
    }
}
